package com.xbfinance.manager.service.impl;


import com.xbfinance.manager.dao.P2pAccountCashMapper;
import com.xbfinance.manager.dao.P2pAccountCashMapperSon;
import com.xbfinance.manager.pojo.dto.*;
import com.xbfinance.manager.pojo.po.P2pAccountCash;
import com.xbfinance.manager.pojo.po.P2pAccountCashExample;
import com.xbfinance.manager.pojo.po.P2pAccountRechargeExample;
import com.xbfinance.manager.service.AdminGetMoneyCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AdminGetMoneyCheckServiceImpl implements AdminGetMoneyCheckService {
    @Autowired
    private P2pAccountCashMapperSon p2pAccountCashMapperSon;
    @Autowired
    private P2pAccountCashMapper p2pAccountCashMapper;
    @Override
    public AdminPageResult<P2pAccountCash> getList(AdminPageParam page) {
        AdminPageResult<P2pAccountCash> apr = new AdminPageResult<P2pAccountCash>();
        int rows=page.getRows();
        apr.setRows(page.getRows());//当前数量  limit
        apr.setPage(page.getPage());//当前页
        try{
            //全部数量
            int count = (int) p2pAccountCashMapperSon.countItems();
            apr.setTotal(count);
            //全部页
            int pages= (int) Math.ceil(1.0*count/rows);
            apr.setPages(pages);
            //全部集合
            List<P2pAccountCash> list = p2pAccountCashMapperSon.listItemsByPage(page);
            System.out.println("offset:"+page.getOffset());
            System.out.println("rows:"+page.getRows());
            System.out.println(list);
            apr.setDocs(list);
        }catch(Exception e){
            e.printStackTrace();
        }
        return apr;
    }

    @Override
    public int deleteOneByCid(int cid) {
        return  p2pAccountCashMapperSon.deleteByPrimaryKey(cid);
    }

    @Override
    public int adminEditByCid(P2pAccountCash p2p) {
        return p2pAccountCashMapperSon.voUpdateByPrimaryKey(p2p);
    }

    @Override
    public AdminPageResult<P2pAccountCash> adminSearch(AdminPageParam page, AdminSearch as) {
        AdminPageResult<P2pAccountCash> result = new AdminPageResult<>();

        int count = p2pAccountCashMapperSon.getSearchCount(as);
        int rows = page.getRows();
        List<P2pAccountCash> list = p2pAccountCashMapperSon.seachDiy(page,as);
        result.setDocs(list);
        result.setPage(page.getPage());
        result.setRows(page.getRows());
        result.setTotal(count);
        int pages= (int) Math.ceil(1.0*count/rows);
        result.setPages(pages);
        return result;
    }

    @Override
    public int agreeByCid(int cid,double money,double fee) {
        P2pAccountCash p = new P2pAccountCash();
        p.setCid(cid);
        p.setCashstatus((byte)1);
        p.setVerifyuser(888);
        p.setVerifytime(new Date());
        double realmoney = money-fee;
        p.setRealmoney(realmoney);
        return p2pAccountCashMapperSon.updateByPrimaryKeySelective(p);
    }

    @Override
    public int rejectByCid(int cid) {
        P2pAccountCash p = new P2pAccountCash();
        p.setCid(cid);
        p.setCashstatus((byte)2);
        p.setVerifyuser(888);
        p.setVerifytime(new Date());
        return p2pAccountCashMapperSon.updateByPrimaryKeySelective(p);
    }

    @Override
    public int addCheck(P2pAccountCash a) {
        return p2pAccountCashMapper.insertSelective(a);

    }

    @Override
    public int adminRemoves(List<Integer> list) {
        P2pAccountCashExample example = new P2pAccountCashExample();
        example.createCriteria().andCidIn(list);
        return p2pAccountCashMapper.deleteByExample(example);

    }


}
